package com.cloudptt.wp.ui;

import com.cloudptt.api.comm.Common;
import com.cloudptt.wp.R;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class WarningActivity extends Activity {

	private Ringtone r = null;
	private WakeLock wakeLock;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.warning_dialog);
		
		ImageView warningImage = (ImageView) findViewById(R.id.warning_img);  
        TextView nameTV = (TextView) findViewById(R.id.warning_name);
        TextView dateTv = (TextView) findViewById(R.id.warning_time);
        dateTv.setText(Common.getDate());
        
        
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        r = RingtoneManager.getRingtone(this, notification);
        r.play();
        
        Button confirmBtn = (Button) findViewById(R.id.warning_confirm_btn);
        confirmBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

        nameTV.setText(getIntent().getStringExtra("msg"));
        
        requestWakeLock();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
		if(r != null)
		{
			r.stop();
			r= null;
		}
		
		releaseWakeLock();
	}
	
	public void requestWakeLock() {
		PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wakeLock = powerManager.newWakeLock(
				PowerManager.SCREEN_BRIGHT_WAKE_LOCK
				| PowerManager.ACQUIRE_CAUSES_WAKEUP,
				"com.csipsimple.onIncomingCall");
		if (wakeLock != null)
			wakeLock.setReferenceCounted(false);
		if (wakeLock != null && !wakeLock.isHeld()) {
			wakeLock.acquire(10000);
		}

		KeyguardManager keyguardManager = (KeyguardManager) this.getSystemService(KEYGUARD_SERVICE);
		KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");

		keyguardLock.reenableKeyguard();
		keyguardLock.disableKeyguard(); // 瑙ｉ攣
	}

	private void releaseWakeLock()
	{
		if (wakeLock != null && wakeLock.isHeld()) {
			wakeLock.release();
		}
	}
		
}
